package com.lilike.music.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author weijun
 */
public final class DateCommonUtils {

    public final static String BASIC_DATE = "yyyy-MM-dd HH:mm:ss";

    public final static String BASIC_DAY = "yyyyMMdd";

    public final static String BASIC_FORMAET_DAY = "yyyy-MM-dd";

    public final static String BASIC_DEFAULT = "yyyyMMddHHmmss";

    public static String currentDay(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(BASIC_DAY);
        return dateTimeFormatter.format(now);
    }

    /**
     *  格式化时间
     * @param date
     * @param format
     * @return
     */
    public static String formatDay(Date date, String format){
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     *  格式化时间
     * @param localDateTime
     * @param format
     * @return
     */
    public static String formatDay(LocalDateTime localDateTime,String format){
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 时间转换格式
     * @param date
     * @return
     */
    public static String formatDate(Date date){
        if( date == null ) return null;
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(BASIC_DATE));
    }

    /**
     * 将字符串类型的日期格式转换成日期对象
     *
     * @param datestr
     * @param format
     * @return
     */
    public static Date convertStringToDate(String datestr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date dt = sdf.parse(datestr);
            return dt;
        } catch (ParseException pe) {
            throw new RuntimeException("Failure while converting [" + datestr +
                    "] to a Date. [format=" + format + "]", pe);
        }
    }

    /**
     * 获取两个日期之间的所有自然日
     *
     * @param sttdt
     *          开始日期
     * @param enddt
     *          结束日期
     * @return
     */
    public static List<Date> findBetweenForDates(Date sttdt, Date enddt) {
        if( sttdt == null || enddt == null ) return null;

        List<Date> list = new ArrayList<Date>();

        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(sttdt);

        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(enddt);

        // 此日期是否在指定日期之后
        while (enddt.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            list.add(calBegin.getTime());
        }
        return list;
    }
}
